package org.jeecg.modules.kc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.kc.entity.KcDepository;
import org.jeecg.modules.kc.entity.KcDepositoryInGoods;
import org.jeecg.modules.kc.service.IKcDepositoryInGoodsService;
import org.jeecg.modules.kc.service.IKcDepositoryService;
import org.jeecg.modules.kc.vo.GoodStorage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

 /**
 * @Description: kc_depository_in_goods
 * @Author: jeecg-boot
 * @Date:   2023-04-18
 * @Version: V1.0
 */
@Api(tags="kc_depository_in_goods")
@RestController
@RequestMapping("/kc/kcDepositoryInGoods")
@Slf4j
public class KcDepositoryInGoodsController extends JeecgController<KcDepositoryInGoods, IKcDepositoryInGoodsService> {

	@Autowired
	private IKcDepositoryService kcDepositoryService;
	/**
	 * 分页列表查询
	 *
	 * @param kcDepositoryInGoods
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "kc_depository_in_goods-分页列表查询")
	@ApiOperation(value="kc_depository_in_goods-分页列表查询", notes="kc_depository_in_goods-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<KcDepositoryInGoods>> queryPageList(KcDepositoryInGoods kcDepositoryInGoods,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		Page<KcDepositoryInGoods> page = new Page<KcDepositoryInGoods>(pageNo, pageSize);
		IPage<KcDepositoryInGoods> pageList = service.depositoryInGoodsList(page, kcDepositoryInGoods);
		pageList.getRecords().forEach(item -> {
			KcDepository kcDepository = kcDepositoryService.getById(item.getDepositoryId());
			if (kcDepository != null) {
				item.setDepositoryName(kcDepository.getName());
			}
		});

		return Result.OK(pageList);
	}

	/**
	 *   添加
	 * @param kcDepositoryInGoods
	 * @return
	 */
	@AutoLog(value = "kc_depository_in_goods-添加")
	@ApiOperation(value="kc_depository_in_goods-添加", notes="kc_depository_in_goods-添加")
	//@RequiresPermissions("kc:kc_depository_in_goods:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody KcDepositoryInGoods kcDepositoryInGoods) {
		service.save(kcDepositoryInGoods);
		return Result.OK("添加成功！");
	}

	 /**
	  *   添加
	  * @param kcDepositoryInGoods
	  * @return
	  */
//	 @AutoLog(value = "kc_depository_in_goods-添加")
//	 @ApiOperation(value="kc_depository_in_goods-添加", notes="kc_depository_in_goods-添加")
//	 //@RequiresPermissions("kc:kc_depository_in_goods:add")
//	 @PostMapping(value = "/addSell")
//	 public Result<String> addSell(@RequestBody KcDepositoryInGoods kcDepositoryInGoods) {
//		 service.addSell(kcDepositoryInGoods);
//		 return Result.OK("添加成功！");
//	 }


	 /**
	 *  编辑
	 *
	 * @param kcDepositoryInGoods
	 * @return
	 */
	@AutoLog(value = "kc_depository_in_goods-编辑")
	@ApiOperation(value="kc_depository_in_goods-编辑", notes="kc_depository_in_goods-编辑")
	//@RequiresPermissions("kc:kc_depository_in_goods:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody KcDepositoryInGoods kcDepositoryInGoods) {
		service.updateById(kcDepositoryInGoods);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "kc_depository_in_goods-通过id删除")
	@ApiOperation(value="kc_depository_in_goods-通过id删除", notes="kc_depository_in_goods-通过id删除")
	//@RequiresPermissions("kc:kc_depository_in_goods:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		service.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "kc_depository_in_goods-批量删除")
	@ApiOperation(value="kc_depository_in_goods-批量删除", notes="kc_depository_in_goods-批量删除")
	//@RequiresPermissions("kc:kc_depository_in_goods:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		service.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "kc_depository_in_goods-通过id查询")
	@ApiOperation(value="kc_depository_in_goods-通过id查询", notes="kc_depository_in_goods-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<KcDepositoryInGoods> queryById(@RequestParam(name="id",required=true) String id) {
		KcDepositoryInGoods kcDepositoryInGoods = service.getById(id);
		if(kcDepositoryInGoods==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(kcDepositoryInGoods);
	}

	 @ApiOperation(value="商品出入库汇总表分页查询", notes="商品出入库汇总表分页查询")
	 @GetMapping(value = "/storagePage")
	 public Result<IPage<GoodStorage>> storagePage(GoodStorage goodStorage,
														   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
														   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
														   HttpServletRequest req) {
		 Page<GoodStorage> page = new Page<>(pageNo, pageSize);
		 return Result.OK(service.storagePage(page, goodStorage));
	 }


	 @ApiOperation(value="商品出入库详情表分页查询", notes="商品出入库详情表分页查询")
	 @GetMapping(value = "/storageDetailPage")
	 public Result<IPage<GoodStorage>> storageDetailPage(GoodStorage goodStorage,
												   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												   HttpServletRequest req) {
		 Page<GoodStorage> page = new Page<>(pageNo, pageSize);
		 return Result.OK(service.storageDetailPage(page, goodStorage));
	 }

	 @ApiOperation(value="商品销售出入库详情表分页查询", notes="商品出入库详情表分页查询")
	 @GetMapping(value = "/goodsSellDetailPage")
	 public Result<IPage<GoodStorage>> goodsSellPage(GoodStorage goodStorage,
												   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												   HttpServletRequest req) {
		 Page<GoodStorage> page = new Page<>(pageNo, pageSize);
		 return Result.OK(service.goodsSellDetailPage(page, goodStorage));
	 }

	 @ApiOperation(value="商品销售出入库汇总表分页查询", notes="商品出入库详情表分页查询")
	 @GetMapping(value = "/goodsSellCollect")
	 public Result<IPage<GoodStorage>> goodsSellCollect(GoodStorage goodStorage,
													 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
													 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
													 HttpServletRequest req) {
		 Page<GoodStorage> page = new Page<>(pageNo, pageSize);
		 return Result.OK(service.goodsSellCollect(page, goodStorage));
	 }



    /**
    * 导出excel
    *
    * @param request
    * @param kcDepositoryInGoods
    */
    //@RequiresPermissions("kc:kc_depository_in_goods:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, KcDepositoryInGoods kcDepositoryInGoods) {
        return super.exportXls(request, kcDepositoryInGoods, KcDepositoryInGoods.class, "kc_depository_in_goods");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("kc:kc_depository_in_goods:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, KcDepositoryInGoods.class);
    }

}
